package Application.Summoner;

import Application.Config.Config;
import Application.Exceptions.AppExceptions;
import Application.Utils.FileOutputUtil;
import Application.Utils.StaticUtil;
import Application.MainMenu;
import Application.MethodObject.DefTings;
import Application.Others.FunnyJoke;
import Application.Widgets.FormattedTextFileds.TextFieldInteger;
import Application.Widgets.TextFileds.TextFiledDouble;
import Application.Widgets.TextFileds.TextFiledString;
import Application.Writer.XamlWriter;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import static Application.Utils.StaticUtil.chooser;

public class ThingSummoner {

    public static void ThingsSummoner()
    {
        DefTings def = new DefTings();
        Config config = Config.configReader();
        FunnyJoke funnyJoke = new FunnyJoke();

        StaticUtil.summonerFrame = new JFrame();
        JFrame mainFrame = StaticUtil.summonerFrame;
        mainFrame.setTitle("边缘世界ModXAML生成器");
        mainFrame.setIconImage(new ImageIcon("src/Asset/Rim-Craft.png").getImage());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        JPanel errPanel = StaticUtil.errorPanel;
        StaticUtil.Init_errorReportPane();
        JButton funnyJokes = new JButton("看看笑话");
        funnyJokes.setFont(config.fontGetter());
        funnyJokes.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StaticUtil.errorReport.append(funnyJoke.getJokes_1() + "\n" + "笑话来源于网络\n");
            }
        });

        JPanel mainPanel = new JPanel();
        BoxLayout Box_Y = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
        mainPanel.setLayout(Box_Y);

        StaticUtil.Init_menuBar(mainFrame);

        JLabel lab_XAMLName = new JLabel("请键入文件名称");
        lab_XAMLName.setFont(config.fontGetter());

        TextFiledString tf_XAMLName = new TextFiledString(def.XAMLName);
        tf_XAMLName.setWidgetName("文件名设置器");
        tf_XAMLName.setFont(config.fontGetter());

        JLabel lab_label = new JLabel("请输入物品显示名");
        lab_label.setFont(config.fontGetter());

        TextFiledString tf_label = new TextFiledString(def.label);
        tf_label.setWidgetName("物品显示名设置器");
        tf_label.setFont(config.fontGetter());

        JLabel lab_defName = new JLabel("请输入物品注册名");
        lab_defName.setFont(config.fontGetter());

        TextFiledString tf_defName = new TextFiledString(def.defName);
        tf_defName.setWidgetName("物品注册名设置器");
        tf_defName.setFont(config.fontGetter());

        JLabel lab_description = new JLabel("请输入物品描述");
        lab_description.setFont(config.fontGetter());

        JTextArea tf_description = new JTextArea(1, 20);
        tf_description.setLineWrap(true);
        tf_description.setFont(config.fontGetter());

        JLabel lab_texPath = new JLabel("请输入材质路径");
        lab_texPath.setFont(config.fontGetter());

        TextFiledString tf_texPath = new TextFiledString(def.texPath);
        tf_texPath.setWidgetName("物品材质路径设置器");
        tf_texPath.setFont(config.fontGetter());

        JLabel lab_stackLimit = new JLabel("请输入堆叠数量");
        lab_stackLimit.setFont(config.fontGetter());

        TextFieldInteger tf_stackLimit = new TextFieldInteger(20);
        tf_stackLimit.setWidgetName("堆叠数量设置栏");
        tf_stackLimit.setFont(config.fontGetter());

        JCheckBox cb_useHitPoints = new JCheckBox("是否启用耐久");
        cb_useHitPoints.setSelected(true);
        cb_useHitPoints.setFont(config.fontGetter());

        JLabel lab_maxHitPoints = new JLabel("请输入物品耐久");
        lab_maxHitPoints.setFont(config.fontGetter());

        TextFieldInteger tf_maxHitPoints = new TextFieldInteger( 20);
        tf_maxHitPoints.setWidgetName("物品耐久设置设置栏");
        tf_maxHitPoints.setText(String.valueOf(def.maxHitPoints));
        tf_maxHitPoints.setFont(config.fontGetter());


        cb_useHitPoints.addChangeListener(e -> {
            if (cb_useHitPoints.isSelected())
            {
                lab_maxHitPoints.setVisible(true);
                tf_maxHitPoints.setVisible(true);
            }
            else
            {
                lab_maxHitPoints.setVisible(false);
                tf_maxHitPoints.setVisible(false);
            }
        });

        JCheckBox cb_selectable = new JCheckBox("是否允许选中");
        cb_selectable.setSelected(true);
        cb_selectable.setFont(config.fontGetter());

        JCheckBox cb_healthAffectsPrice = new JCheckBox("是否允许耐久度影响价格");
        cb_healthAffectsPrice.setFont(config.fontGetter());

        JLabel lab_marketValue = new JLabel("请输入物品贸易价格");
        lab_marketValue.setFont(config.fontGetter());

        TextFieldInteger tf_marketValue = new TextFieldInteger( 20);
        tf_marketValue.setWidgetName("物品贸易价格设置栏");
        tf_marketValue.setFont(config.fontGetter());

        JLabel lab_mass = new JLabel("请输入物品质量");
        lab_mass.setFont(config.fontGetter());

        TextFiledDouble tf_mass = new TextFiledDouble(def.mass);
        tf_mass.setWidgetName("物品质量设置器");
        tf_mass.setToolTipText("该值为一个双标浮点数（即带有小数位的数字，也就是Double类型），范围：0.1~1.0");
        tf_mass.setFont(config.fontGetter());

        JLabel lab_flammability = new JLabel("请输入物品燃烧系数");
        lab_flammability.setFont(config.fontGetter());

        TextFiledDouble tf_flammability = new TextFiledDouble(def.flammability);
        tf_flammability.setFont(config.fontGetter());

        JLabel lab_deteriorationRate = new JLabel("请输入物品劣化系数");
        lab_deteriorationRate.setFont(config.fontGetter());

        TextFiledDouble tf_deteriorationRate = new TextFiledDouble(def.deteriorationRate);
        tf_deteriorationRate.setFont(config.fontGetter());

        JCheckBox cb_alwaysHaulable = new JCheckBox("是否可被携带");
        cb_alwaysHaulable.setSelected(true);
        cb_alwaysHaulable.setFont(config.fontGetter());

        JCheckBox cb_drawGUIOverlay = new JCheckBox("是否在选中时加载GUI");
        cb_drawGUIOverlay.setSelected(true);
        cb_drawGUIOverlay.setFont(config.fontGetter());

        JCheckBox cb_rotatable = new JCheckBox("是否允许旋转");
        cb_rotatable.setFont(config.fontGetter());

        JLabel lab_pathCost = new JLabel("请输入物品减速系数");
        lab_pathCost.setFont(config.fontGetter());

        TextFieldInteger tf_pathCost = new TextFieldInteger( 20);
        tf_pathCost.setFont(config.fontGetter());

        JLabel lab_thingCategories = new JLabel("请输入物品储存区类别");
        lab_thingCategories.setFont(config.fontGetter());

        JTextArea tf_thingCategories = new JTextArea(1, 20);
        tf_thingCategories.setLineWrap(true);
        tf_thingCategories.setText("Manufactured");
        tf_thingCategories.setFont(config.fontGetter());

        JLabel lab_tag = new JLabel("请输入物品标签");
        lab_tag.setFont(config.fontGetter());

        JTextArea tf_tag = new JTextArea(1, 20);
        tf_tag.setLineWrap(true);
        tf_tag.setFont(config.fontGetter());

        JLabel lab_thingSetMakerTags = new JLabel("请输入物品品质奖励标准");
        lab_thingSetMakerTags.setFont(config.fontGetter());

        JTextArea tf_thingSetMakerTags = new JTextArea(1, 20);
        tf_thingSetMakerTags.setLineWrap(true);
        tf_thingSetMakerTags.setFont(config.fontGetter());

        JButton choice_save_path = new JButton("请选择默认打开路径");
        choice_save_path.setFont(config.fontGetter());
        choice_save_path.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser path_chooser = new JFileChooser();
                path_chooser.setFont(config.fontGetter());
                path_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                path_chooser.setDialogTitle("选择");
                path_chooser.setAcceptAllFileFilterUsed(false);
                path_chooser.addChoosableFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        return f.isDirectory();
                    }

                    @Override
                    public String getDescription() {
                        return "目录";
                    }
                });
                path_chooser.showOpenDialog(mainFrame);
                config.default_open_path = path_chooser.getSelectedFile();
            }
        });

        JButton summon = new JButton();
        summon.setVisible(true);
        summon.setText("单击以生成XAML文件");
        summon.setSize(100, 30);
        summon.setFont(config.fontGetter());
        summon.addActionListener(e -> {
            def.XAMLName = tf_XAMLName.getValue();
            def.defName = tf_defName.getValue();
            def.label = tf_label.getValue();
            if (!tf_description.getText().equals("") && tf_description.getText() != null) {
                def.description = tf_description.getName();
            }
            def.texPath = tf_texPath.getValue();
            def.stackLimit = tf_stackLimit.getInteger(def.stackLimit);
            def.useHitPoints = cb_useHitPoints.isSelected();
            if (cb_useHitPoints.isSelected()) {
                def.maxHitPoints = tf_maxHitPoints.getInteger(def.maxHitPoints);
            }
            def.selectable = cb_selectable.isSelected();
            def.healthAffectsPrice = cb_healthAffectsPrice.isSelected();
            def.marketValue = tf_marketValue.getInteger(def.marketValue);
            def.mass = tf_mass.getValue(0,1);
            def.flammability = tf_flammability.getValue(0);
            def.deteriorationRate = tf_deteriorationRate.getValue(0,10);
            def.alwaysHaulable = cb_alwaysHaulable.isSelected();
            def.drawGUIOverlay = cb_drawGUIOverlay.isSelected();
            def.rotatable = cb_rotatable.isSelected();
            def.pathCost = tf_pathCost.getInteger(def.pathCost);
            if (!tf_thingCategories.getText().equals("") && tf_thingCategories.getText() != null) {
                def.thingCategories = tf_thingCategories.getText();
            }
            if (!tf_tag.getText().equals("") && tf_tag.getText() != null) {
                def.tags = tf_tag.getText();
            }
            if (!tf_thingSetMakerTags.getText().equals("") && tf_thingSetMakerTags.getText() != null) {
                def.thingCategories = tf_thingSetMakerTags.getText();
            }

            FileOutputUtil.Output(mainFrame, def.defName, () ->
            {
                try {
                    XamlWriter.ThingsWriter(def, chooser.getSelectedFile());
                } catch (IOException ex) {
                    ex.printStackTrace();
                    StaticUtil.errorReport.append(ex.getMessage());
                }
            });
        });

        mainPanel.add(lab_XAMLName);
        mainPanel.add(tf_XAMLName);
        mainPanel.add(lab_label);
        mainPanel.add(tf_label);
        mainPanel.add(lab_defName);
        mainPanel.add(tf_defName);
        mainPanel.add(lab_description);
        mainPanel.add(tf_description);
        mainPanel.add(lab_texPath);
        mainPanel.add(tf_texPath);
        mainPanel.add(lab_stackLimit);
        mainPanel.add(tf_stackLimit);
        mainPanel.add(cb_useHitPoints);
        mainPanel.add(lab_maxHitPoints);
        mainPanel.add(tf_maxHitPoints);
        mainPanel.add(cb_selectable);
        mainPanel.add(cb_healthAffectsPrice);
        mainPanel.add(lab_marketValue);
        mainPanel.add(tf_marketValue);
        mainPanel.add(lab_mass);
        mainPanel.add(tf_mass);
        mainPanel.add(lab_flammability);
        mainPanel.add(tf_flammability);
        mainPanel.add(lab_deteriorationRate);
        mainPanel.add(tf_deteriorationRate);
        mainPanel.add(cb_alwaysHaulable);
        mainPanel.add(cb_drawGUIOverlay);
        mainPanel.add(cb_rotatable);
        mainPanel.add(lab_pathCost);
        mainPanel.add(tf_pathCost);
        mainPanel.add(lab_thingCategories);
        mainPanel.add(tf_thingCategories);
        mainPanel.add(lab_tag);
        mainPanel.add(tf_tag);
        mainPanel.add(lab_thingSetMakerTags);
        mainPanel.add(tf_thingSetMakerTags);
        mainPanel.add(choice_save_path);
        mainPanel.add(summon);
        mainPanel.add(funnyJokes);

        mainFrame.setSize(1200, 500);
        mainFrame.add(mainPanel, BorderLayout.CENTER);
        mainFrame.add(errPanel, BorderLayout.EAST);
        mainFrame.setVisible(true);
     }
    }

